using NUnit.Framework;
using Tycho.Parser.Peg;

namespace Tycho.Parser.Tests.Peg {
    [TestFixture]
    public class OperatorKeywordProductionSpecification {
        [Test]
        public void ShouldParseOperator () {
            AssertParseOperator ("+", "+");
            AssertParseOperator ("+", "+(");
            AssertParseOperator ("+", "+a");
            AssertParseOperator ("+", "+9");
        }

        [Test]
        public void ShouldNotParseOperatorNextToOtherOperator () {
            AssertNotParseOperator ("+", "++");
            AssertNotParseOperator ("+", "+-");
            AssertNotParseOperator ("+", "+/");
            AssertNotParseOperator ("+", "+$");
        }

        [Test]
        public void NameShouldReturnOperatorKeyword () {
            var op = OperatorKeywordProduction.CreateTerminal ("*");

            Assert.That (op.Name, Is.EqualTo ("'*'"));
        }

        private static void AssertParseOperator (string op, string sourceString) {
            ParseResult result = ParseOperator (op, sourceString);

            Assert.IsNotNull (result);
            Assert.AreEqual (op.Length, result.Index);
            Assert.IsNull (result.Term);
        }

        private static void AssertNotParseOperator (string op, string sourceString) {
            ParseResult result = ParseOperator (op, sourceString);

            Assert.IsNull (result);
        }

        private static ParseResult ParseOperator (string op, string source) {
            var id = OperatorKeywordProduction.CreateTerminal (op);

            return id.Parse (source, 0, new ParseContext ());
        }
    }
}